Hệ thống quản lý nguồn nhân lực Dự án năm cuối trong c#
1 Public Class Frm_Department
2
3 Dim deptt_id
4
5 Private Sub loadAllManagers()
6
7 Dim query As String = "Select manager_id, manager_name From Manager order by manager_name"
8 Dim dtEmp As DataTable = getDataTable(query)
9
10 cmboxManagers.DataSource = dtEmp
11 cmboxManagers.DisplayMember = "manager_name"
12 cmboxManagers.ValueMember = "manager_id"
13
14 If cmboxManagers.Items.Count > 0 Then
15 cmboxManagers.SelectedIndex = 0
16 End If
17
18 End Sub
19
20 Function emptyFields()
21
22 If txtName.Text = "" OrElse txtPhone.Text = "" OrElse txtStrength.Text = "" Then
23
24 Return True
25
26 End If
27
28 Return False
29
30 End Function
31
32 Sub formClear()
33
34 txtName.Clear()
35 txtPhone.Clear()
36 txtStrength.Clear()
37
38 End Sub
39
40 Private Function autoId() As Integer ' generating auto employee id for a new employee
41
42 Dim query = "Select IsNull(Max(department_id+1),0) department_id From Department"
43 Dim dr As SqlClient.SqlDataReader
44 dr = getDataReader(query)
45 dr.Read()
46 deptt_id = dr("department_id")
47 dr.Close()
48
49 Return deptt_id
50
51 End Function
52
53 Private Sub add()
54
55 If emptyFields() = True Then
56
57 MsgBox("Must Enter In All Fields ", MsgBoxStyle.Information)
58
59 Return
60
61 Else
62
63 dgvDeptt.Rows.Add()
64 dgvDeptt.Rows(dgvDeptt.RowCount - 1).Cells("DepttID").Value = txtID.Text
65 dgvDeptt.Rows(dgvDeptt.RowCount - 1).Cells("DepttName").Value = txtName.Text
66 dgvDeptt.Rows(dgvDeptt.RowCount - 1).Cells("DepttPhone").Value = txtPhone.Text
67 dgvDeptt.Rows(dgvDeptt.RowCount - 1).Cells("DepttStrength").Value = txtStrength.Text
68 dgvDeptt.Rows(dgvDeptt.RowCount - 1).Cells("DepttManID").Value = cmboxManagers.SelectedValue
69 dgvDeptt.Rows(dgvDeptt.RowCount - 1).Cells("DepttManName").Value = cmboxManagers.Text
70 dgvDeptt.Rows(dgvDeptt.RowCount - 1).DefaultCellStyle.ForeColor = Color.Blue
71
72 End If
73
74 formClear()
75
76 End Sub
77
78 Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
79 Try
80 add()
81 Catch ex As Exception
82 MsgBox(ex.Message, MsgBoxStyle.Critical)
83 End Try
84 End Sub
85
86 Private Sub edit()
87
88 If dgvDeptt.SelectedRows.Count = 0 Then
89
90 Return
91
92 End If
93
94 If btnEdit.Text = "Edit" Then
95
96 txtID.Text = dgvDeptt.SelectedRows(0).Cells("DepttID").Value
97 txtName.Text = dgvDeptt.SelectedRows(0).Cells("DepttName").Value
98 txtPhone.Text = dgvDeptt.SelectedRows(0).Cells("DepttPhone").Value
99 txtStrength.Text = dgvDeptt.SelectedRows(0).Cells("DepttStrength").Value
100 cmboxManagers.SelectedValue = dgvDeptt.SelectedRows(0).Cells("DepttManID").Value
101 cmboxManagers.Text = dgvDeptt.SelectedRows(0).Cells("DepttManName").Value
102 dgvDeptt.Enabled = False
103 btnEdit.Text = "Update"
104
105 Else
106
107 dgvDeptt.SelectedRows(0).Cells("DepttID").Value = txtID.Text
108 dgvDeptt.SelectedRows(0).Cells("DepttName").Value = txtName.Text
109 dgvDeptt.SelectedRows(0).Cells("DepttPhone").Value = txtPhone.Text
110 dgvDeptt.SelectedRows(0).Cells("DepttStrength").Value = txtStrength.Text
111 dgvDeptt.SelectedRows(0).Cells("DepttManID").Value = cmboxManagers.SelectedValue
112 dgvDeptt.SelectedRows(0).Cells("DepttManName").Value = cmboxManagers.Text
113 dgvDeptt.Enabled = True
114 btnEdit.Text = "Edit"
115
116
117 End If
118
119 dgvDeptt.SelectedRows(0).DefaultCellStyle.ForeColor = Color.Blue
120
121 End Sub
122
123 Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
124 Try
125 edit()
126 Catch ex As Exception
127 MsgBox(ex.Message, MsgBoxStyle.Critical)
128 End Try
129 End Sub
130
131 Private Sub remove()
132
133 If dgvDeptt.SelectedRows.Count = 0 Then
134
135 Return
136
137 End If
138
139 dgvDeptt.SelectedRows(0).DefaultCellStyle.ForeColor = Color.Red
140
141 End Sub
142
143 Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
144 Try
145 remove()
146 Catch ex As Exception
147 MsgBox(ex.Message, MsgBoxStyle.Critical)
148 End Try
149 End Sub
150
151 Private Sub apply()
152
153 Dim query As String = String.Empty
154
155 For Each row As DataGridViewRow In dgvDeptt.Rows
156
157 If row.Cells("FromDB").Value = True Then
158
159 If row.DefaultCellStyle.ForeColor = Color.Black Then
160
161 Continue For
162
163 ElseIf row.DefaultCellStyle.ForeColor = Color.Blue Then
164
165 query = "Update Department set department_name = '" & row.Cells("DepttName").Value & "',strength = '" & row.Cells("DepttStrength").Value & "',phone_number = '" & row.Cells("DepttPhone").Value & "',manager_id = '" & row.Cells("DepttManID").Value & "' Where department_id = '" & row.Cells("DepttID").Value & "'"
166
167 ElseIf row.DefaultCellStyle.ForeColor = Color.Red Then
168
169 query = "Delete From Department Where department_id = " & row.Cells("DepttID").Value
170
171 End If
172
173
174 Else
175
176 If row.DefaultCellStyle.ForeColor = Color.Blue Then
177
178 query = "Insert Into Department (department_name,strength,phone_number,manager_id) Values ('" & row.Cells("DepttName").Value & "','" & row.Cells("DepttStrength").Value & "','" & row.Cells("DepttPhone").Value & "','" & row.Cells("DepttManID").Value & "')"
179
180 ElseIf row.DefaultCellStyle.ForeColor = Color.Red Then
181
182 dgvDeptt.Rows.Remove(row)
183
184 Continue For
185
186 End If
187
188 End If
189
190 executeQuery(query)
191
192 Next
193
194 reload()
195
196 End Sub
197
198 Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click
199 Try
200 apply()
201 Catch ex As Exception
202 MsgBox(ex.Message, MsgBoxStyle.Critical)
203 End Try
204 End Sub
205
206 Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
207 Try
208 apply()
209 Me.Close()
210 Catch ex As Exception
211 MsgBox(ex.Message, MsgBoxStyle.Critical)
212 End Try
213 End Sub
214
215 Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
216 Me.Close()
217 End Sub
218
219 Private Sub reload()
220
221 Dim query As String
222 query = "Select D.*, M.manager_name, M.manager_id"
223 query = query + " From Department D "
224 query = query + " Inner Join Manager M On D.manager_id = M.manager_id"
225
226 dgvDeptt.Rows.Clear()
227
228 Dim dt As DataTable = getDataTable(query)
229
230 For Each row As DataRow In dt.Rows
231
232 dgvDeptt.Rows.Add()
233 dgvDeptt.Rows(dgvDeptt.RowCount - 1).Cells("DepttID").Value = row("department_id")
234 dgvDeptt.Rows(dgvDeptt.RowCount - 1).Cells("DepttName").Value = row("department_name")
235 dgvDeptt.Rows(dgvDeptt.RowCount - 1).Cells("DepttStrength").Value = row("strength")
236 dgvDeptt.Rows(dgvDeptt.RowCount - 1).Cells("DepttPhone").Value = row("phone_number")
237 dgvDeptt.Rows(dgvDeptt.RowCount - 1).Cells("DepttManID").Value = row("manager_id")
238 dgvDeptt.Rows(dgvDeptt.RowCount - 1).Cells("DepttManName").Value = row("manager_name")
239 dgvDeptt.Rows(dgvDeptt.RowCount - 1).Cells("FromDB").Value = True
240 dgvDeptt.Rows(dgvDeptt.RowCount - 1).DefaultCellStyle.ForeColor = Color.Black
241
242 Next
243
244 txtID.Text = autoId()
245
246 End Sub
247
248 Private Sub Frm_Department_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
249 Try
250 loadAllManagers()
251 'txtID.Text = autoId()
252 reload()
253 Catch ex As Exception
254 MsgBox(ex.Message, MsgBoxStyle.Critical)
255 End Try
256 End Sub
257
258 Private Sub txtName_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtName.KeyPress
259 Dim ch As Char = e.KeyChar
260 If Char.IsDigit(ch) Then 'Ristricting Deptt name To Input Only Characters
261 e.Handled = True
262 End If
263 End Sub
264
265 Private Sub txtPhone_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPhone.KeyPress
266 Dim ch As Char = e.KeyChar
267 If Char.IsLetter(ch) Then 'Ristricting TxtPhone To Input Only Digits(any number)
268 e.Handled = True
269 End If
270 End Sub
271
272 Private Sub txtStrength_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtStrength.KeyPress
273 Dim ch As Char = e.KeyChar
274 If Char.IsLetter(ch) Then 'Ristricting strength To Input Only Digits(any number)
275 e.Handled = True
276 End If
277 End Sub
278 End Class